package mashuaiproject.mashuaiproject.controller;

import mashuaiproject.mashuaiproject.dto.PaginationDto;
import mashuaiproject.mashuaiproject.dto.QuesionUserDto;
import mashuaiproject.mashuaiproject.mapper.NotificationMapper;
import mashuaiproject.mashuaiproject.mapper.QuesionMapper;
import mashuaiproject.mashuaiproject.mapper.UserMapper;
import mashuaiproject.mashuaiproject.model.Quesions;
import mashuaiproject.mashuaiproject.model.Users;
import mashuaiproject.mashuaiproject.serivce.JMontiorSerivce;
import mashuaiproject.mashuaiproject.serivce.QuesionUserSerivce;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private QuesionUserSerivce quesionUserSerivce;
    @Autowired
    private QuesionMapper quesionMapper;
    @GetMapping("/") //get请求。。@PostMapping是post请求
    public  String index(HttpServletRequest request, Model model
        , @RequestParam(name = "page",defaultValue = "1")Integer page
        , @RequestParam(name ="size",defaultValue = "6") Integer size,
                         @RequestParam(value = "sort",required = false) String sort
                         ,@RequestParam(value = "serachQuesions",required = false)String serachQuesions){
        System.out.println("sort:"+sort);
        //查找出当前的问题信息
        PaginationDto paginationDto= quesionUserSerivce.selectQ_UList(page,size,serachQuesions,sort);//根据serachQuesions是不是值进行查找
        //查找前五个热门话题
        List<Quesions> hotQuesions=quesionUserSerivce.hotQuesions();

        //查找排行榜
        List<Users> rankIngUsers=userMapper.selecAllUser();
        model.addAttribute("hotQuesions",hotQuesions);
        model.addAttribute("paginationDto",paginationDto);
        model.addAttribute("sort",sort);
        model.addAttribute("rankIngUsers",rankIngUsers);
        return "index";
    }

    @GetMapping("/about")
    public  String aboutour(){
        return "about";
    }
    @ResponseBody
    @GetMapping("/searchValue")
    public  List<Quesions > searchValues(@RequestParam("inputMes")String inputMes){
        //
        List<Quesions> quesionsList=quesionMapper.selectByTitle(inputMes);

        return quesionsList;
    }

}

